package com.ruoyi.lowcode.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.constant.CacheNames;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.common.utils.redis.CacheUtils;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.lowcode.domain.bo.LcDictionaryBo;
import com.ruoyi.lowcode.domain.vo.LcDictionaryVo;
import com.ruoyi.lowcode.domain.LcDictionary;
import com.ruoyi.lowcode.mapper.LcDictionaryMapper;
import com.ruoyi.lowcode.service.ILcDictionaryService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 数据字典Service业务层处理
 *
 * @author ruoyi
 * @date 2023-09-28
 */
@RequiredArgsConstructor
@Service
public class LcDictionaryServiceImpl implements ILcDictionaryService {

    private final LcDictionaryMapper baseMapper;

    /**
     * 查询数据字典
     */
    @Override
    public LcDictionaryVo queryById(Long dictionaryId){
        return baseMapper.selectVoById(dictionaryId);
    }

    /**
     * 查询数据字典列表
     */
    @Override
    public TableDataInfo<LcDictionaryVo> queryPageList(LcDictionaryBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<LcDictionary> lqw = buildQueryWrapper(bo);
        Page<LcDictionaryVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询数据字典列表
     */
    @Override
    public List<LcDictionaryVo> queryList(LcDictionaryBo bo) {
        LambdaQueryWrapper<LcDictionary> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<LcDictionary> buildQueryWrapper(LcDictionaryBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<LcDictionary> lqw = Wrappers.lambdaQuery();
        lqw.like(StringUtils.isNotBlank(bo.getDictionaryName()), LcDictionary::getDictionaryName, bo.getDictionaryName());
        lqw.eq(StringUtils.isNotBlank(bo.getType()), LcDictionary::getType, bo.getType());
        lqw.eq(StringUtils.isNotBlank(bo.getContent()), LcDictionary::getContent, bo.getContent());
        return lqw;
    }

    /**
     * 新增数据字典
     */
    @Override
    public Boolean insertByBo(LcDictionaryBo bo) {
        LcDictionary add = BeanUtil.toBean(bo, LcDictionary.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setDictionaryId(add.getDictionaryId());
        }
        return flag;
    }

    /**
     * 修改数据字典
     */
    @Override
    public Boolean updateByBo(LcDictionaryBo bo) {
        LcDictionary update = BeanUtil.toBean(bo, LcDictionary.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(LcDictionary entity){
        //TODO 做一些数据校验,如唯一约束
        // 删除列表缓存
        CacheUtils.evict(CacheNames.LOW_CODE, "dictionary_queryList_all");
    }

    /**
     * 批量删除数据字典
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
